﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using G15_Agent.Models.Airplane;

namespace G15_Agent.Helpers.ServiceHelpers.AirplaneServiceHelpers
{
    public class AirplaneService : Service
    {
        virtual public List<SanBay> IGetDepartAirport()
        {
            return null;
        }

        virtual public List<SanBay> IGetArrivalAirport()
        {
            return null;
        }

        virtual public List<LoaiVe> IGetTicketType()
        {
            return null;
        }

        virtual public double? IGetTicketPrice(
            string ticketTypeID, 
            SanBay departure, 
            SanBay arrival, 
            string tripType, 
            DateTime departDate, 
            string departTime)
        {
            return null;    
        }

        virtual public HoaDonMayBay IBookingTicket(
            string ticketTypeID, 
            DateTime departDate, 
            DateTime returnDate,
            string tripType, 
            string departAirportID, 
            string arriveAirportID, 
            List<KhachHang> listPassenger)
        {
            return null;
        }

        virtual public List<ChuyenBay> ISearchForTicket(
            int numberOfChilren, 
            int numberOfAdult, 
            string ticketTypeID,
            DateTime departDate, 
            DateTime returnDate, 
            string tripType, 
            string departAirport,
            string arriveAirport)
        {
            return null;
        }

        public List<SanBay> GetDepartAirport()
        {
            string key =
                "ServiceID" + ServiceID +
                "GetDepartAirport";

            List<SanBay> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<SanBay>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IGetDepartAirport();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IGetDepartAirport();
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }

        public List<SanBay> GetArrivalAirport()
        {
            string key =
                "ServiceID" + ServiceID +
                "GetArriveAirport";

            List<SanBay> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<SanBay>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IGetArrivalAirport();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IGetArrivalAirport();
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }

        public List<LoaiVe> GetTicketType()
        {
            string key =
                "ServiceID" + ServiceID +
                "GetTicketType";

            List<LoaiVe> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<LoaiVe>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IGetTicketType();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IGetTicketType();
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }

        public double? GetTicketPrice(
            string ticketTypeID,
            SanBay departure,
            SanBay arrival,
            string tripType,
            DateTime departDate,
            string departTime)
        {
            string key =
                "ServiceID" + ServiceID + 
                "ticketTypeID" + ticketTypeID +
                "departure" + departure.maSanBay +
                "arrival" + arrival.maSanBay +
                "tripType" + tripType +
                "departDate" + departDate.ToShortDateString() +
                "departTime" + departTime;

            double? result = null;
            // Kiểm tra dữ liệu trong cache
            result = (double?)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IGetTicketPrice(
                        ticketTypeID,
                        departure,
                        arrival,
                        tripType,
                        departDate,
                        departTime);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IGetTicketPrice(
                            ticketTypeID,
                            departure,
                            arrival,
                            tripType,
                            departDate,
                            departTime);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }

        public HoaDonMayBay BookingTicket(
            string ticketTypeID,
            DateTime departDate,
            DateTime returnDate,
            string tripType,
            string departAirportID,
            string arriveAirportID,
            List<KhachHang> listPassenger)
        {
            HoaDonMayBay result = null;
            
            try
            {
                if (!isLogin)
                    this.Login();
                result = this.IBookingTicket(
                    ticketTypeID,
                    departDate,
                    returnDate,
                    tripType,
                    departAirportID,
                    arriveAirportID,
                    listPassenger);
            }
            catch (Exception ex)
            {
                if (this.Login())
                    result = this.IBookingTicket(
                    ticketTypeID,
                    departDate,
                    returnDate,
                    tripType,
                    departAirportID,
                    arriveAirportID,
                    listPassenger);
            }

            return result;
        }

        public List<ChuyenBay> SearchForTicket(
            int numberOfChilren,
            int numberOfAdult,
            string ticketTypeID,
            DateTime departDate,
            DateTime returnDate,
            string tripType,
            string departAirport,
            string arriveAirport)
        {
            string key =
                "numberOfChilren" + numberOfChilren +
                "numberOfAdult" + numberOfAdult +
                "ticketTypeID" + ticketTypeID +
                "departDate" + departDate.ToShortDateString() +
                "returnDate" + returnDate.ToShortDateString() +
                "tripType" + tripType +
                "departAirport" + departAirport +
                "arriveAirport" + arriveAirport;

            List<ChuyenBay> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<ChuyenBay>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.ISearchForTicket(
                        numberOfChilren,
                        numberOfAdult,
                        ticketTypeID,
                        departDate,
                        returnDate,
                        tripType,
                        departAirport,
                        arriveAirport);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.ISearchForTicket(
                        numberOfChilren,
                        numberOfAdult,
                        ticketTypeID,
                        departDate,
                        returnDate,
                        tripType,
                        departAirport,
                        arriveAirport);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }
    }
}